import store from '@/store'
import { Toast } from 'vant'
import http from '@/utils/request'
import debounce from 'lodash/debounce'
export default {
  data () {
  },
  activated () {
  },
  created () {
  },
  watch: {
  },
  methods: {
    /*
     *@author wf_Huang
     *@Time 2019/8/16 0016 16:25
     *@function  统计PV
     *****************************************/
    setPv () {
      http({
        url: `${window.SITE_CONFIG['mediaURL']}/wpfansmeta/addCountPv?openId=${store.state.openid}`,
        method: 'get',
        withCredentials: false
      }).then(({ data: res }) => {
        // this.showLoding = false
      }).catch(() => {})
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 23:03
     *@function  注册成粉丝
     *****************************************/
    setRegistered () {
      http({
        url: `${window.SITE_CONFIG['mediaURL']}/wpfansmeta/registerFans`,
        method: 'post',
        data: {
          fansDvice: store.state.device, // APP/WX/H5
          fansChannel: store.state.gsign, // 渠道标识(gkzx)
          fansUniacId: store.state.uniacid, // 微信ID(渠道ID)
          fassLogo: store.state.media.logoUrl, // 渠道logo
          fansShareId: store.state.media.shareId, // 分享者ID
          fansPostId: parseInt(store.state.media.articleId), // 文章ID
          openid: store.state.openid // 文章ID
        },
        withCredentials: false
      }).then(({ data: res }) => {
        console.log('收获一个粉丝')
      }).catch(() => {})
    },
    /*
    *@author wf_Huang
    *@Time 2019/8/13 0013 11:51
    *@function  微信授权
    *****************************************/
    getWx () {
      var pageUrl = encodeURIComponent(window.location.href)
      window.location.href = `http://wechat.eenet.com/eeapi.php?myaction=oauth&uniacid=${this.$route.query.uniacid}&scope=base&backurl=${pageUrl}`
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/13 0013 14:40
     *@function  获取访问地址上的参数
     *****************************************/
    getUrlData () {
      // http://localhost:8017/#/media-find?device=WX&gsign=gkzx&uniacid=39&logoUrl=logo&shareId=33&articleId=22
      // ?device=APP/WX/H5&gsign=渠道标识(gkzx)&uniacid=微信ID(渠道ID)&logoUrl=渠道logo&shareId=分享者ID&articleId=文章ID
      // 是否进行判断的条件
      if (this.$route.query.device && this.$route.query.gsign && this.$route.query.uniacid) {
        if (this.$route.query.device === 'WX') {
          if (store.state.openid) { // 判断是否有本地openid
            if (store.state.uniacid !== this.$route.query.uniacid || store.state.gsign !== this.$route.query.gsign) {
              store.state.uniacid = this.$route.query.uniacid
              store.state.gsign = this.$route.query.gsign
              localStorage.setItem('stateData', JSON.stringify(store.state))
              this.getWx()
            } else {
              this.setRegistered()
              this.showLoding = false
            }
          } else {
            if (this.$route.query.openid) { // 判断地址有没有openid
              store.state.openid = this.$route.query.openid
              localStorage.setItem('stateData', JSON.stringify(store.state))
              console.log(store.state.openid)
              this.setRegistered()
              this.showLoding = false
            } else {
              this.getWx()
            }
          }
          /* if (this.$route.query.openid) {
            store.state.openid = this.$route.query.openid
            if (!this.$route.query.shareId) {
              store.state.media.shareId = this.$route.query.openid
            }
            // 注册粉丝
            this.setRegistered()
            this.showLoding = false
          } else {
            // 不是同一个微信公众号
            this.getWx()
          } */
        } else if (this.$route.query.device === 'APP') {
          if (this.$route.query.openid) {
            store.state.openid = this.$route.query.openid
            console.log(store.state.openid)
            localStorage.setItem('stateData', JSON.stringify(store.state))
          }
          // 注册粉丝
          this.setRegistered()
          this.showLoding = false
        }
        store.state.device = this.$route.query.device
        store.state.gsign = this.$route.query.gsign
        store.state.uniacid = this.$route.query.uniacid
        store.state.media.shareId = this.$route.query.shareId || this.$route.query.openid
        store.state.media.articleId = this.$route.query.articleId || 0
        localStorage.setItem('stateData', JSON.stringify(store.state))
      } else {
        this.showLoding = false
      }
      // 统计PV
      this.setPv()
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/13 0013 15:50
     *@function 自媒体关注功能
     *****************************************/
    setAttention (boggerId, item) {
      if (item.isConcerned === 1) {
        http({
          url: `${window.SITE_CONFIG['mediaURL']}/wpfans/addFan?openId=${store.state.openid}&postId=${item.post ? item.post.id : 0}`, // 文章ID
          method: 'post',
          data: {
            fansAuthorId: boggerId, // 关注的当前博主ID
            fansDvice: store.state.device, // APP/WX/H5
            fansChannel: store.state.gsign, // 渠道标识(gkzx)
            fansId: 0,
            fansUniacId: store.state.uniacid, // 微信ID(渠道ID)
            fassLogo: store.state.media.logoUrl, // 渠道logo
            fansShareId: store.state.media.shareId // 分享者ID
          },
          withCredentials: false
        }).then(({ data: res }) => {
          if (res.code !== 200) {
            Toast('关注失败')
          }
          Toast('关注成功')
          item.isConcerned = 0
          return true
        }).catch(() => {})
      } else {
        // Toast('已经关注')
      }
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 9:13
     *@function  文章点赞
     *****************************************/
    setLike: debounce(function (articleId, item) {
      if (item.idCompliment === 1) {
        http({
          url: `${window.SITE_CONFIG['mediaURL']}/wpminapperext/addExt?postId=${articleId}`,
          method: 'post',
          data: {
            fansDvice: store.state.device, // APP/WX/H5
            fansChannel: store.state.gsign, // 渠道标识(gkzx)
            fansUniacId: store.state.uniacid, // 微信ID(渠道ID)
            fassLogo: store.state.media.logoUrl, // 渠道logo
            fansShareId: store.state.media.shareId, // 分享者ID
            openid: store.state.openid
          },
          withCredentials: false
        }).then(({ data: res }) => {
          item.extidCount = item.extidCount + 1
          item.idCompliment = 0
          return true
        }).catch(() => {})
      } else {
        // 点赞取消
      }
    }, 1000, { 'leading': true, 'trailing': false }),
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 9:13
     *@function  视屏点赞
     *****************************************/
    setLikeVideo (articleId, item) {
      http({
        url: `${window.SITE_CONFIG['mediaURL']}/courses/courseFabulous?videoId=${articleId}`,
        method: 'get',
        withCredentials: false
      }).then(({ data: res }) => {
        item.agree = parseInt(item.agree) + 1
        return true
      }).catch(() => {})
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 10:54
     *@function  获取用户广场
     *****************************************/
    getUserList () {
      http({
        url: `${window.SITE_CONFIG['mediaURL']}/wpusers/changeUserPlaza?openId=${store.state.openid}`,
        method: 'get',
        withCredentials: false
      }).then(({ data: res }) => {
        this.userList = res.data || []
        return true
      }).catch(() => {})
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 19:33
     *@function  打开专业报读
     *****************************************/
    openRead () {
      window.location.href = 'https://m.oucgz.cn/web/professionalListNew?gsign=' + store.state.gsign + '&from=singlemessage'
    },
    /*
     *@author wf_Huang
     *@Time 2019/8/14 0014 20:32
     *@function  打开个人中心
     *****************************************/
    openPersonal () {
      this.$router.push({ path: '/personal-authorize',
        query: { device: store.state.device,
          gsign: store.state.gsign,
          uniacid: store.state.uniacid,
          logoUrl: store.state.media.logoUrl,
          shareId: store.state.media.shareId,
          articleId: store.state.media.articleId } })
    }
  }
}
